1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Data.SqlClient;
10 namespace WarehouseManagementSystem
11 {
12 public partial class frmSubCategory : Form
13 {
14 SqlDataReader rdr = null;
15 DataTable dtable = new DataTable();
16 SqlConnection con = null;
17 SqlCommand cmd = null;
18 DataTable dt = new DataTable();
19 ConnectionString cs = new ConnectionString();
20 public frmSubCategory()
21 {
22 InitializeComponent();
23 }
24
25
26 private void Reset()
27 {
28 txtSubCategory.Text = "";
29
30 btnSave.Enabled = true;
31 btnDelete.Enabled = false;
32 btnUpdate.Enabled = false;
33 txtSubCategory.Focus();
34 }
35
36 private void btnNew_Click(object sender, EventArgs e)
37 {
38 Reset();
39 }
40
41 private void btnSave_Click(object sender, EventArgs e)
42 {
43
44 if (txtSubCategory.Text == "")
45 {
46 MessageBox.Show("Please enter sub Category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
47 txtSubCategory.Focus();
48 return;
49 }
50
51
52 try
53 {
54 con = new SqlConnection(cs.DBConn);
55 con.Open();
56 string ct = "select SubCategoryName from SubCategory where SubCategoryName='" + txtSubCategory.Text + "'";
57
58 cmd = new SqlCommand(ct);
59 cmd.Connection = con;
60 rdr = cmd.ExecuteReader();
61
62 if (rdr.Read())
63 {
64 MessageBox.Show("SubCategory Name Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
65 txtSubCategory.Text = "";
66 txtSubCategory.Focus();
67
68
69 if ((rdr != null))
70 {
71 rdr.Close();
72 }
73 return;
74 }
75
76 con = new SqlConnection(cs.DBConn);
77 con.Open();
78
79 string cb = "insert into SubCategory(SubCategoryName,CategoryID) VALUES ('" + txtSubCategory.Text + "'," + txtCategoryID.Text + ")";
80 cmd = new SqlCommand(cb);
81 cmd.Connection = con;
82 cmd.ExecuteReader();
83 con.Close();
84 MessageBox.Show("Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
85 Autocomplete();
86 btnSave.Enabled = false;
87
88 }
89 catch (Exception ex)
90 {
91 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
92 }
93 }
94 private void Autocomplete()
95 {
96 try
97 {
98 con = new SqlConnection(cs.DBConn);
99 con.Open();
100 SqlCommand cmd = new SqlCommand("SELECT distinct SubCategoryName FROM SubCategory", con);
101 DataSet ds = new DataSet();
102 SqlDataAdapter da = new SqlDataAdapter(cmd);
103 da.Fill(ds, "SubCategory");
104 AutoCompleteStringCollection col = new AutoCompleteStringCollection();
105 int i = 0;
106 for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
107 {
108 col.Add(ds.Tables[0].Rows[i]["SubCategoryName"].ToString());
109
110 }
111 txtSubCategory.AutoCompleteSource = AutoCompleteSource.CustomSource;
112 txtSubCategory.AutoCompleteCustomSource = col;
113 txtSubCategory.AutoCompleteMode = AutoCompleteMode.Suggest;
114
115 con.Close();
116 }
117 catch (Exception ex)
118 {
119 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
120 }
121 }
122
123 private void btnDelete_Click(object sender, EventArgs e)
124 {
125 if (MessageBox.Show("Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
126 {
127 delete_records();
128 }
129 }
130 private void delete_records()
131 {
132
133 try
134 {
135
136 int RowsAffected = 0;
137 con = new SqlConnection(cs.DBConn);
138 con.Open();
139 string cq = "delete from SubCategory where ID=" + txtSubCategoryID.Text + "";
140 cmd = new SqlCommand(cq);
141 cmd.Connection = con;
142 RowsAffected = cmd.ExecuteNonQuery();
143 if (RowsAffected > 0)
144 {
145 MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
146 Reset();
147 Autocomplete();
148 }
149 else
150 {
151 MessageBox.Show("No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information);
152 Reset();
153 Autocomplete();
154 }
155 if (con.State == ConnectionState.Open)
156 {
157 con.Close();
158 }
159
160
161 }
162 catch (Exception ex)
163 {
164 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
165 }
166 }
167
168 private void btnUpdate_Click(object sender, EventArgs e)
169 {
170 try
171 {
172 if (txtSubCategory.Text == "")
173 {
174 MessageBox.Show("Please enter sub Category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
175 txtSubCategory.Focus();
176 return;
177 }
178 con = new SqlConnection(cs.DBConn);
179 con.Open();
180
181 string cb = "update SubCategory set SubCategoryName='" + txtSubCategory.Text + "',CategoryID=" + txtCategoryID.Text + " where ID=" + txtSubCategoryID.Text + "";
182 cmd = new SqlCommand(cb);
183 cmd.Connection = con;
184 cmd.ExecuteReader();
185 con.Close();
186 MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
187 Autocomplete();
188 btnUpdate.Enabled = false;
189 }
190 catch (Exception ex)
191 {
192 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
193 }
194 }
195
196 private void btnGetData_Click(object sender, EventArgs e)
197 {
198 this.Hide();
199 frmSubCategoryRecord frm = new frmSubCategoryRecord();
200 frm.Show();
201 frm.GetData();
202 }
203
204
205 private void frmSubCategory_Load(object sender, EventArgs e)
206 {
207 Autocomplete();
208 FillCombo();
209 }
210 public void FillCombo()
211 {
212 try
213 {
214
215 con = new SqlConnection(cs.DBConn);
216 con.Open();
217 string ct = "select RTRIM(CategoryName) from Category order by CategoryName";
218 cmd = new SqlCommand(ct);
219 cmd.Connection = con;
220 rdr = cmd.ExecuteReader();
221
222 while (rdr.Read())
223 {
224 cmbCategory.Items.Add(rdr[0]);
225 }
226 con.Close();
227 }
228 catch (Exception ex)
229 {
230 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
231 }
232 }
233
234 private void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)
235 {
236 try
237 {
238 con = new SqlConnection(cs.DBConn);
239
240 con.Open();
241 cmd = con.CreateCommand();
242
243 cmd.CommandText = "SELECT ID from Category WHERE CategoryName = '" + cmbCategory.Text + "'";
244 rdr = cmd.ExecuteReader();
245
246 if (rdr.Read())
247 {
248 txtCategoryID.Text = rdr.GetInt32(0).ToString().Trim();
249 }
250 if ((rdr != null))
251 {
252 rdr.Close();
253 }
254 if (con.State == ConnectionState.Open)
255 {
256 con.Close();
257 }
258
259
260 }
261 catch (Exception ex)
262 {
263 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
264 }
265 }
266
267 }
268 }